﻿using CCWin;
using COMMON.Http;
using COMMON.Http.Sender;
using COMMON.Json;
using EBQComm;
using 易保全;
using ENCRYPT.sha512;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Net;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;

namespace 易保全
{
    public partial class MainForm : CCSkinMain
    {
        private ScreenRecorderHelper srh = null;
        private IEForm frmIe = null;
        private int pageIndex = 1;
        private int pageSize = AppInfo.PageSize;
        private int pageCount = 0;
        private int allCount = 0;
        System.Threading.Timer SessionRefrshTimer;
        private string uploadFileName;
        private List<MyPreserve> lst = new List<MyPreserve>();
        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
        public MainForm()
        {
            if (!userLogin()) System.Environment.Exit(0);//登录被取消退出系统 
            Control.CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();
            this.pbStart.Visible = true;
            this.pbStop.Visible = false;
            this.pbOpFirst.Visible = true;
            this.pbAuthed.Visible = false;
            this.pbUnAuthed.Visible = true;
            //展现当前用户信息
            cbbType.SelectedIndex = 0;
           // displayLoginUser();
            //检查版本
            new System.Threading.Thread(CheckVersion).Start();
        }

        public ScreenRecorderHelper getScreenRecorderHelper() {
            return srh;
        }
        /// <summary>
        /// 检查版本
        /// </summary>
        private void CheckVersion()
        {
            try
            {
                Dictionary<string, object> paras = new Dictionary<string, object>();
                paras.Add("deviceType", 3);
                PostSender sender = new PostSender(AppInfo.VersionCheckUrl, paras, null);
                HttpWebResponse rsp = sender.send(AppInfo.Delay, null, null);
                String resInfo = ResponseHelper.GetResponseString(rsp);
                Dictionary<string, object> o = JSONResponseParser.parse(resInfo) as Dictionary<string, object>;
                object success = getDictValue("success", o);
                if (bool.Parse(success.ToString()))
                {
                    Dictionary<string, object> result = getDictValue("result", o) as Dictionary<string, object>;
                    String version = getDictValue("version", result).ToString();
                    String updateAdress = getDictValue("updateAdress", result).ToString();
                    if(version != AppInfo.AppVersion){
                        //需要更新
                        new UpdateForm(updateAdress).ShowDialog();
                    }
                }
            }
            catch { 
                
            }
        }
        /// <summary>
        /// 展现当前用户信息
        /// </summary>
        private void displayLoginUser()
        {
            if (AppInfo.LoginStatus)
            {
                SessionRefrshTimer = new System.Threading.Timer(RefreshSession, null, 0, 1000 * 60 * 25);
                this.dataGridView1.Visible = true;
                panPager.Visible = true;
                this.lblGrvTip.Visible = false;
                this.pbAuthed.Visible = true;
                this.pbUnAuthed.Visible = false;
                btnLogin.Text = "退出登录";
                lblLoginUser.Text = AppInfo.UserName;
                new System.Threading.Thread(showPic).Start();
            }
            else
            {
                if (SessionRefrshTimer != null)
                {
                    SessionRefrshTimer.Dispose();
                }
                this.dataGridView1.Visible = false;
                panPager.Visible = false;
                this.lblGrvTip.Text = "您还没登录系统";
                this.pbAuthed.Visible = false;
                this.pbUnAuthed.Visible = true;
                this.lblGrvTip.Visible = true;
                btnLogin.Text = "点击登录";
                lblLoginUser.Text = "<未登录>";
                pbUserPic.Image = ((System.Drawing.Image)(resources.GetObject("pbUserPic.ErrorImage")));
            }
        }

        private void showPic(object obj)
        {
            Image pic = null;
            try
            {
                string picurl = AppInfo.ImagePath;
                pic = Image.FromStream(WebRequest.Create(picurl).GetResponse().GetResponseStream());
            }
            catch { }
            if (pic != null) pbUserPic.Image = pic;
        }
        /// <summary>
        /// ping接口--刷新Session
        /// </summary>
        private void RefreshSession(object state)
        {
            Dictionary<string, object> header = new Dictionary<string, object>();
            header.Add("Cookie", AppInfo.Cookies);
            PostSender sender = new PostSender(AppInfo.PingUrl, null, header);
            sender.send(50000, null, null);
        }
        /// <summary>
        /// 用户登录
        /// </summary>
        private bool userLogin()
        {
            LoginForm frmLogin = new LoginForm();
            try
            {
                DialogResult result = frmLogin.ShowDialog();

                return (result == DialogResult.OK);
            }
            finally
            {
                frmLogin.Dispose();
            }
        }
        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (AppInfo.LoginStatus)
            {
                System.Environment.Exit(0);
            }
            else
            {
                doLogin();//打开登录窗口
            }
            displayLoginUser();
        }
        /// <summary>
        /// 退出登录
        /// </summary>
        private void doLogoff()
        {
            if (AppInfo.TakeEvidenceFlag)
            {
                MessageBox.Show("正在取证,请结束取证后退出登录！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            //退出登录成功
            doClearAppInfo();
        }
        private void doClearAppInfo()
        {
            AppInfo.AppKey = string.Empty;
            AppInfo.AppSecret = string.Empty;
            AppInfo.ImagePath = string.Empty;
            AppInfo.LoginStatus = false;
            AppInfo.UserId = string.Empty;
            AppInfo.UserName = string.Empty;
            AppInfo.AuthStatus = string.Empty;
        }
        /// <summary>
        /// 用户登录
        /// </summary>
        private void doLogin()
        {
            LoginForm frm = new LoginForm();

            DialogResult rlt = frm.ShowDialog();
            if (rlt == DialogResult.OK)
            {
                AppInfo.LoginStatus = true;//登录成功
            }
        }
        /// <summary>
        /// 完成取证任务
        /// </summary>
        public void stopTask()
        {
            srh.stop();
            //切换为:开始取证,显示查看录像
            AppInfo.TakeEvidenceFlag = false;
            this.pbStart.Visible = true;
            this.pbStop.Visible = false;
            this.label18.Text = "开始录像";
            //send hash
            if (AppInfo.Cancled) {
                new FrmMsg("系统提示", "取证过程使用了其他浏览器，本次取证将不会被保全").ShowDialog();
                return;
            }
            new System.Threading.Thread(doPostHash).Start();
        }
        private void doPostHash()
        {
            try
            {
                String zipPath = srh.recordsPath + "/" + "保全存根文件,请勿删除.zip";
                #region 压缩文件
                string[] files = Directory.GetFiles(srh.recordsPath);
                ZipOutputStream s = new ZipOutputStream(File.Create(zipPath));
                s.SetLevel(9);
                byte[] buffer = new byte[4096];
                foreach(string file in files){
                    ZipEntry entry = new ZipEntry(Path.GetFileName(file));
                    entry.DateTime = DateTime.Now;
                    s.PutNextEntry(entry);
                    using (FileStream fs = File.OpenRead(file))
                    {
                        int sourceBytes;
                        do
                        {
                            sourceBytes = fs.Read(buffer, 0, buffer.Length);
                            s.Write(buffer, 0, sourceBytes);
                        } while (sourceBytes > 0);
                    }
                }
                s.Finish();
                s.Close();
                #endregion 压缩文件

                //先压缩
                String hash = SHA512Encoder.encodeFile(zipPath);
                IDictionary<String, object> paras = new Dictionary<string, object>();
                paras.Add("userId", AppInfo.UserId);
                paras.Add("hashValue", hash);
                paras.Add("preservationType", (int)易保全.AppInfo.PreservationType.VideoProof);
                paras.Add("name", AppInfo.PreserveName);
                paras.Add("description", AppInfo.PreserveRemark);
                paras.Add("isShow", 1);
                Dictionary<string, object> header = new Dictionary<string, object>();
                header.Add("Cookie", AppInfo.Cookies);
                PostSender sender_ = new PostSender(AppInfo.PreservationHashURLEnt, paras, header);

                HttpWebResponse res = sender_.send(AppInfo.Delay, null, null);
                String resInfo = ResponseHelper.GetResponseString(res);
                Dictionary<string, object> o = JSONResponseParser.parse(resInfo) as Dictionary<string, object>;

                object statusInfo = getDictValue("success", o);
                if (bool.Parse(statusInfo.ToString()))
                {
                    Dictionary<string, object> data = getDictValue("result", o) as Dictionary<string, object>;
                    new FrmMsg("系统提示", "录像取证成功,录像保全号为:" + getDictValue("preservationId", data)).ShowDialog();
                }
                else
                {
                    new FrmMsg("系统提示", "录像取证保全失败!").ShowDialog();
                }
            }
            catch
            {
                new FrmMsg("系统提示", "录像取证保全失败!").ShowDialog();
            }
        }
        private object getDictValue(string key, Dictionary<string, object> dict)
        {
            object ret = null;
            bool flag = dict.TryGetValue(key, out ret);
            return ret;
        }
        /// <summary>
        /// 取消取证任务
        /// </summary>
        public void cancleTask()
        {
            srh.stop();
            //切换正在取证标识
            AppInfo.TakeEvidenceFlag = false;
            this.pbStart.Visible = true;
            this.pbStop.Visible = false;
            this.label18.Text = "开始录像";
            //TODO 删除录像文件
        }
        private void pbStart_Click(object sender, EventArgs e)
        {
            if (AppInfo.LoginStatus == false)
            {
                new FrmMsg("系统提示", "您还没有登录系统,请登录！").ShowDialog();
                return;
            }
            PictureBox pb = (PictureBox)sender;


            if (AppInfo.TakeEvidenceFlag == false)
            {
                //保全须知 提示窗口
                if (new PreserveNoteWebForm().ShowDialog() == DialogResult.Cancel) return;

                //浏览器进程检测
                if (!BrowserChecker.Check()) {
                    new FrmMsg("系统提示", "请关闭其他浏览器再进行取证操作").ShowDialog();
                    return;
                }

                //备注 名称  输入窗口
                InputBoxForm ipt = new InputBoxForm("系统提示", "请设置取证录像的备注信息:", 50, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                if (ipt.ShowDialog() == DialogResult.Cancel) return;
                AppInfo.PreserveRemark = ipt.RecordRemark;
                AppInfo.PreserveName = ipt.RecordName;
                //系统初始化进度条
                //ProcessBarForm tmp = new ProcessBarForm(5, "正在初始化系统环境,请稍候...");
                ProcessBarForm tmp = new ProcessBarForm(5,10, "正在初始化系统环境,请稍候...");
                if (tmp.ShowDialog() == DialogResult.Cancel) return;

                //网络初始化
                tmp = new ProcessBarForm(5, 10, "正在初始化网络,请稍候...");
                if (tmp.ShowDialog() == DialogResult.Cancel) return;
                //录像初始化
                tmp = new ProcessBarForm(5, 10, "正在初始化屏幕取证录像机,请稍候...");
                if (tmp.ShowDialog() == DialogResult.Cancel) return;

                frmIe = new IEForm(this);
                frmIe.Show();

                this.pbStart.Visible = (pb == pbStop);
                this.pbStop.Visible = (pb == pbStart);
                this.label18.Text = "停止录像";
                AppInfo.TakeEvidenceFlag = true;

                String time = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss");
                //初始化屏幕录像机
                String path = System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);

                srh = new ScreenRecorderHelper(path + "\\EBQ\\records\\" + time, 8, Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
                srh.start();
            }
            else
            {
                if (frmIe != null && sender != null)
                {
                    frmIe.Close();
                }
                this.pbStart.Visible = (pb == pbStop);
                this.pbStop.Visible = (pb == pbStart);
                this.label18.Text = "开始录像";
                //刷新界面
                AppInfo.TakeEvidenceFlag = false;
                //停止取证
                srh.stop();
                //发送ahsh
                new System.Threading.Thread(doPostHash).Start();
            }
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            displayLoginUser();
            this.pageIndex = 1;
            step1.Visible = true;
            step2.Visible = false;
            step3.Visible = false;
            step4.Visible = false;
            //doBindDataGridViewData();
        }
        /// <summary>
        /// 设置分页控制按钮
        /// </summary>
        private void doSetPager(int pageIndex, int pageSize, int allCount, int PageCount)
        {
            this.lblCurrentPage.Text = pageIndex.ToString();
            this.lblPageCount.Text = PageCount.ToString();
            this.lblTotalCount.Text = allCount.ToString();
            this.txtPageNum.Text = pageIndex.ToString();

            this.linkFirst.Enabled = (pageIndex != 1);
            this.linkPrevious.Enabled = (pageIndex > 1);
            this.linkNext.Enabled = (pageIndex < PageCount);
            this.linkLast.Enabled = (pageIndex != PageCount);
        }
        /// <summary>
        /// 加载全部保全数据
        /// </summary>

        private void doBindDataGridViewData()
        {
            this.Invoke(new Action(delegate
            {
                //TODO 这个时候可以开线程来跑数据了
                Dictionary<string, object> o = null;
                lst = new List<MyPreserve>();
                try
                {
                    Dictionary<String, object> paras = new Dictionary<string, object>();
                    paras.Add("page", this.pageIndex);
                    paras.Add("pageSize", this.pageSize);

                    Dictionary<string, object> header = new Dictionary<string, object>();
                    header.Add("Cookie", AppInfo.Cookies);

                    PostSender sender = new PostSender(AppInfo.FindPreservationURL, paras, header);

                    HttpWebResponse res = sender.send(AppInfo.Delay, null, null);
                    String resInfo = ResponseHelper.GetResponseString(res);
                    o = JSONResponseParser.parse(resInfo) as Dictionary<string, object>;
                    object statusInfo = getDictValue("success", o);
                    if (bool.Parse(statusInfo.ToString()))
                    {
                        //接口成功获取真正的数据
                        Dictionary<string, object> data = getDictValue("result", o) as Dictionary<string, object>;
                        Dictionary<string, object> pager = getDictValue("pager", data) as Dictionary<string, object>;

                        Newtonsoft.Json.JavaScriptArray preservationInfos = getDictValue("preservations", data) as Newtonsoft.Json.JavaScriptArray;

                        allCount = int.Parse(getDictValue("items", pager).ToString());
                        this.pageCount = int.Parse(getDictValue("pages", pager).ToString());
                        foreach (var item in preservationInfos)
                        {
                            Dictionary<string, object> itemData = item as Dictionary<string, object>;
                            MyPreserve d = new MyPreserve();
                            DateTime start = DateTime.Parse("1970-01-01 00:08:00");
                            DateTime time = doConvertUTC(long.Parse(getDictValue("preservationTime", itemData).ToString()));
                            d.PreserveTime = time.ToString("yyyy-MM-dd HH:mm:ss");
                            d.Name = getDictValue("preservationTitle", itemData).ToString();
                            d.UUID = FillZore(getDictValue("preservationId", itemData).ToString());
                            d.Status = getPreserveStatusNameByCode(getDictValue("status", itemData).ToString());
                            d.PreserveType = getPreserveTypeNameByCode(getDictValue("preservationType", itemData).ToString());
                            lst.Add(d);
                        }
                    }
                }
                catch
                {
                    new FrmMsg("系统提示", "获取我的保全失败!").ShowDialog();
                }
                //无保全数据
                if (lst.Count == 0)
                {
                    this.dataGridView1.Visible = false;
                    this.panPager.Visible = false;
                    this.lblGrvTip.Text = "您还没有保全信息";
                    this.lblGrvTip.Visible = true;
                    return;
                }
                this.dataGridView1.DataSource = lst;
                doSetPager(this.pageIndex, this.pageSize, this.allCount, this.pageCount);
                this.Cursor = Cursors.Default;
            }));
        }

        private string FillZore(string uuid)
        {
            string target = "";
            int length = uuid.Length >= 10 ? 0 : 10 - uuid.Length;
            for (int i = 0; i < length; i++)
            {
                target += "0";
            }
            return target + uuid;
        }
        private DateTime doConvertUTC(long utc)
        {
            DateTime dt_1970 = new DateTime(1970, 1, 1);
            long tricks_1970 = dt_1970.Ticks;//1970年1月1日刻度
            long time_tricks = tricks_1970 + utc * 10000;//日志日期刻度
            return new DateTime(time_tricks).AddHours(8);//转化为DateTime
        }
        private string getPreserveTypeNameByCode(string p)
        {
            int i = int.Parse(p);
            switch (i)
            {
                case 0:
                    return "网页设计";
                case 1:
                    return "平面设计";
                case 2:
                    return "投影作品";
                case 3:
                    return "视频作品";
                case 4:
                    return "音乐作品";
                case 5:
                    return "电子合同";
                case 6:
                    return "电子证据";
                case 7:
                    return "录像取证";
                default:
                    return "其他";
            }
        }
        private string getPreserveStatusNameByCode(string p)
        {
            int i = int.Parse(p);
            switch (i)
            {
                case 1:
                    return "正常";
                case 2:
                    return "已删除";
                default:
                    return "已转移";
            }
        }
        private class MyPreserve
        {
            public MyPreserve()
            { }
            public string Name { get; set; }
            public string UUID { get; set; }
            public string Status { get; set; }
            public string PreserveType { get; set; }
            public string PreserveTime { get; set; }

        }
        private void pbFirst_Click(object sender, EventArgs e)
        {
            PictureBox pb = (PictureBox)sender;
            //pbOpFirst.Visible = (pb == pbFirst);
            //pbOpSecond.Visible = (pb == pbSecond);
            //pbOpThree.Visible = (pb == pbThree);

        }
        private void pbLook_Click(object sender, EventArgs e)
        {
            String path = System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\EBQ\\records";
            System.Diagnostics.Process.Start("explorer.exe", path);
        }
        private void linkFirst_Click(object sender, EventArgs e)
        {
            disablePager();

            this.pageIndex = 1;
            doBindDataGridViewData();
        }
        private void linkPrevious_Click(object sender, EventArgs e)
        {
            disablePager();

            this.pageIndex = pageIndex - 1;
            doBindDataGridViewData();

        }
        private void linkNext_Click(object sender, EventArgs e)
        {
            disablePager();
            this.pageIndex = pageIndex + 1;
            doBindDataGridViewData();
        }
        private void linkLast_Click(object sender, EventArgs e)
        {
            disablePager();

            this.pageIndex = this.pageCount;
            doBindDataGridViewData();
        }
        private void linkGo_Click(object sender, EventArgs e)
        {
            disablePager();

            try
            {
                int page = int.Parse(this.txtPageNum.Text);
                if (page >= this.pageCount)
                {
                    this.pageIndex = this.pageCount;
                }//页码越界则取最后一页
                else
                {
                    this.pageIndex = page;
                }
                doBindDataGridViewData();
            }
            catch (Exception)
            {
                new FrmMsg("系统提示", "请输入正确页码!").ShowDialog();
            }
        }
        private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            dataGridView1.EndEdit();
        }
        private void dataGridView1_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            dataGridView1.EndEdit();
        }
        private void skinTC1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (skinTC1.SelectedIndex == 0)
            {
                //刷新GridView
                this.pageIndex = 1;
                doBindDataGridViewData();
            }
        }


        private void disablePager()
        {
            this.Cursor = Cursors.WaitCursor;
            this.linkFirst.Enabled = false;
            this.linkPrevious.Enabled = false;
            this.linkNext.Enabled = false;
            this.linkLast.Enabled = false;
        }

        private void linkWebSite_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start(AppInfo.WebSite);
        }

        private void label4_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start(AppInfo.WebSite);
        }

        private void label5_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start(AppInfo.WebSite);
        }

        private void pictureBox5_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("iexplore.exe", "tencent://message/?uin=19754300");
        }

        private void button2_Click(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {

        }

        private void label16_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start(AppInfo.QQUrl);
        }

        private void label20_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start(AppInfo.EmailUrl);
        }

        private void label21_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start(AppInfo.WebSite);
        }

        private void label11_Click(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            String path = System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\EBQ\\records";
            ofd.InitialDirectory = path;
            if (ofd.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
            {
                string file = ofd.FileName;
                this.textBox1.Text = file;
            }
        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            if(!AppInfo.LoginStatus){
                new FrmMsg("系统提示", "您还没有登录").ShowDialog();
                return;  
            }
            button2.Enabled = false;
            button2.Text = "正在验证...";
            string beianhao = this.tbBeianhao.Text.Trim();
            if (beianhao.Length == 0 || beianhao == " 请填写备案号")
            {
                new FrmMsg("系统提示", "请输入备案号").ShowDialog();
                return;
            }
            String file = this.textBox1.Text.Trim();
            if (file.Length == 0 || file == " 请选择文件" || !File.Exists(file))
            {
                new FrmMsg("系统提示", "请选择要验证的文件").ShowDialog();
                return;
            }
            this.Invoke(new Action(delegate
            {
                this.Cursor = Cursors.WaitCursor;
                //计算Hash并调用接口验证
                try
                {
                    Dictionary<string, object> paras = new Dictionary<string, object>();
                    paras.Add("file",file);
                    paras.Add("beianhao", beianhao);
                    new System.Threading.Thread(DoCheckVersion).Start(paras);
                }
                catch
                {
                    button2.Enabled = true;
                    button2.Text = "开始验证";
                    //判断状态
                    this.Cursor = Cursors.Default;
                    new FrmMsg("系统提示", "验证出错,请稍后重试...").ShowDialog();
                }
            }));
        }

        private void DoCheckVersion(object obj)
        {
            Dictionary<string, object> paras_ = obj as Dictionary<string, object>;
            String hash = SHA512Encoder.encodeFile(getDictValue("file", paras_).ToString());
            Dictionary<string, object> paras = new Dictionary<string, object>();
            paras.Add(AppInfo.Hash, hash);
            paras.Add(AppInfo.Beianhao, getDictValue("beianhao", paras_).ToString());
            Dictionary<string, object> header = new Dictionary<string, object>();
            header.Add("Cookie", AppInfo.Cookies);
            PostSender sender_ = new PostSender(AppInfo.VerificationPreservation, paras, header);
            //发送请求出去就好了
            HttpWebResponse res = sender_.send(AppInfo.Delay, null, null);
            String resInfo = ResponseHelper.GetResponseString(res);
            Dictionary<string, object> o = JSONResponseParser.parse(resInfo) as Dictionary<string, object>;
            object statusInfo = getDictValue("success", o);
            bool flag = bool.Parse(statusInfo.ToString());
            button2.Enabled = true;
            button2.Text = "开始验证";
            //判断状态
            this.Cursor = Cursors.Default;
            new FrmMsg("系统提示", flag ? "验证成功" : "验证失败").ShowDialog();
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridView view = sender as DataGridView;
            Clipboard.SetText(view.CurrentCell.Value.ToString());
        }
        private int curImageIndex = 0;
        private void pictureBox10_Click(object sender, EventArgs e)
        {
            curImageIndex++;
            int index = curImageIndex % 3;
            if (index == 0)
            {
                pbOpFirst.Visible = true;
                pbOpSecond.Visible = false;
                pbOpThree.Visible = false;
            }
            if (index == 1)
            {
                pbOpFirst.Visible = false;
                pbOpSecond.Visible = true;
                pbOpThree.Visible = false;
            }
            if (index == 2)
            {
                pbOpFirst.Visible = false;
                pbOpSecond.Visible = false;
                pbOpThree.Visible = true;
            }
        }

        private void pictureBox9_Click(object sender, EventArgs e)
        {
            if (curImageIndex <= 0) curImageIndex = 999;
            curImageIndex--;
            int index = curImageIndex % 3;
            if (index == 0)
            {
                pbOpFirst.Visible = true;
                pbOpSecond.Visible = false;
                pbOpThree.Visible = false;
            }
            if (index == 1)
            {
                pbOpFirst.Visible = false;
                pbOpSecond.Visible = true;
                pbOpThree.Visible = false;
            }
            if (index == 2)
            {
                pbOpFirst.Visible = false;
                pbOpSecond.Visible = false;
                pbOpThree.Visible = true;
            }
        }
        private int curStep = 0;
        private void pictureBox14_Click(object sender, EventArgs e)
        {
            curStep++;
            int index = curStep % 4;
            if (index == 0)
            {
                step1.Visible = true;
                step2.Visible = false;
                step3.Visible = false;
                step4.Visible = false;
            }
            if (index == 1)
            {
                step1.Visible = false;
                step2.Visible = true;
                step3.Visible = false;
                step4.Visible = false;
            }
            if (index == 2)
            {
                step1.Visible = false;
                step2.Visible = false;
                step3.Visible = true;
                step4.Visible = false;
            }
            if (index == 3)
            {
                step1.Visible = false;
                step2.Visible = false;
                step3.Visible = false;
                step4.Visible = true;
            }
        }

        private void pictureBox15_Click(object sender, EventArgs e)
        {
            if (curStep <= 0) curStep = 1000;
            curStep--;
            int index = curStep % 4;
            if (index == 0)
            {
                step1.Visible = true;
                step2.Visible = false;
                step3.Visible = false;
                step4.Visible = false;
            }
            if (index == 1)
            {
                step1.Visible = false;
                step2.Visible = true;
                step3.Visible = false;
                step4.Visible = false;
            }
            if (index == 2)
            {
                step1.Visible = false;
                step2.Visible = false;
                step3.Visible = true;
                step4.Visible = false;
            }
            if (index == 3)
            {
                step1.Visible = false;
                step2.Visible = false;
                step3.Visible = false;
                step4.Visible = true;
            }
        }

        private void tbBeianhao_Enter(object sender, EventArgs e)
        {
            if (tbBeianhao.Text == " 请填写备案号")
                tbBeianhao.Text = "";
        }

        private void button5_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
            {
                string file = ofd.FileName;
                this.textBox4.Text = file;
                uploadFileName = ofd.SafeFileName;
            }
        }

        private void button3_Click_1(object sender, EventArgs e)
        {
           int type = cbbType.SelectedIndex;//保全类型
           string file = textBox4.Text;//文件
           if (type == 0) { 
            //未选择保全类型
               new FrmMsg("系统提示", "请选择保全类型").ShowDialog();
               return;
           }
            if(file==null||file=="请选择文件"|| !File.Exists(file)){
                new FrmMsg("系统提示", "请选择保全文件").ShowDialog();
                return;
            }
            //处理保全提交
            IDictionary<string, object> paras = new Dictionary<string, object>();
            paras.Add("type", type);
            paras.Add("file", file);
            new System.Threading.Thread(upload).Start(paras);
        }

        private void upload(object obj) {
            Dictionary<string, object> paras_ = obj as Dictionary<string, object>;

            try
            {
                String hash = SHA512Encoder.encodeFile( getDictValue("file",paras_).ToString());
                IDictionary<String, object> paras = new Dictionary<string, object>();
                paras.Add("userId", AppInfo.UserId);
                paras.Add("hashValue", hash);
                paras.Add("preservationType", int.Parse( getDictValue("type", paras_).ToString())-1);

                paras.Add("name", uploadFileName);//取文件名为保全名称
                paras.Add("description", "客户端上传的文件保全");
                paras.Add("isShow", 1);
                Dictionary<string, object> header = new Dictionary<string, object>();
                header.Add("Cookie", AppInfo.Cookies);
                PostSender sender_ = new PostSender(AppInfo.PreservationHashURLEnt, paras, header);

                HttpWebResponse res = sender_.send(AppInfo.Delay, null, null);
                String resInfo = ResponseHelper.GetResponseString(res);
                Dictionary<string, object> o = JSONResponseParser.parse(resInfo) as Dictionary<string, object>;

                object statusInfo = getDictValue("success", o);
                if (bool.Parse(statusInfo.ToString()))
                {
                    Dictionary<string, object> data = getDictValue("result", o) as Dictionary<string, object>;
                    new FrmMsg("系统提示", "上传保全成功,保全号为:" + getDictValue("preservationId", data)).ShowDialog();
                }
                else
                {
                    new FrmMsg("系统提示", getDictValue("msg", o).ToString()).ShowDialog();
                }
            }
            catch
            {
                new FrmMsg("系统提示", "上传保全失败!").ShowDialog();
            }
        }
    }
}